package org.alfalfa.component;

import common.AccountProcess;
import common.IAccountProcess;
import common.Logger;
import common.SocksProxy;
import common.StringUtils;
import location.Farm;
import messages.Base;
import messages.EndClientTurn;
import messages.EndClientTurnOK;
import messages.GoHome;
import messages.Login;
import messages.LoginOK;
import messages.OwnCityData;
import messages.OwnFishingData;
import messages.OwnHomeData;
import messages.Unknown;
import messages.VisitedHomeData;
import objects.LogicEventData;
import objects.LogicEventDataEntry;
import objects.MysteryBoxState;
import objects.OrderEntry;

import java.util.Date;
import java.util.Iterator;

/**
 * @Author : Max-Goday
 * @Created : 2016-10-21
 * @E-mail : goday.max@gmail.com
 * @Version : 1.0
 * @Copyright Alfalfa.cc
 */
public class APInstance implements IAccountProcess {

	private Login profile;
	public AccountProcess ap;
	public final SocksProxy socks;
	public final String name;
	public Farm farm = null;

	private boolean showSyslog = true;
	private Date lastReload;

	public void setShowSyslog(boolean showSyslog) {
		this.showSyslog = showSyslog;
	}

	public APInstance(Login profile, SocksProxy socks, String name) {
		this.profile = profile;
		this.socks = socks;
		this.name = name;

		this.ap = new AccountProcess(this.name, this.profile, this.socks, this);
		this.ap.setDetailedLog(true);
//		this.ap.start();

	}

	@Override
	public void ownHomeDataEvent(Long AccountID, Long HomeID, OwnHomeData ownHomeData) {

		syslog("****************************************");
		syslog("************ownHomeDataEvent************");
		syslog("****************************************");

		this.lastReload = new Date();
		int groupIndex = AccountID.intValue() % LogicEventData.getEvent(0).getGroupSize().intValue();
		String sevt = new String();

		for(int i = 0; i < 11; ++i) {
			LogicEventDataEntry oes = LogicEventData.getEvent(i);
			Integer weight = oes.getByGroup(groupIndex);
			sevt = sevt + "  \"" + oes.getGoodReward() + "\": " + weight + " ";
		}

		this.syslog(Logger.formatPT(this.name, "  EventGroupIndex: " + groupIndex + " { " + sevt + "}"));
		if(this.farm == null) {
			this.farm = new Farm(ownHomeData, this.name);
		} else {
			this.farm.update(ownHomeData);
		}

		this.farm.reload(false);
		Iterator var9 = this.farm.getOrders().iterator();

		while(var9.hasNext()) {
			String var10 = ((OrderEntry)var9.next()).toString();
			if(var10 != null) {
//				this.syslog(Logger.formatPT(this.name, "  " + var10));
			}
		}

	}

	@Override
	public void ownCityDataEvent(Long AccountID, Long HomeID, OwnCityData ownCityData) {
		syslog("****************************************");
		syslog("************ownCityDataEvent************");
		syslog("****************************************");

	}

	@Override
	public void ownFishingDataEvent(Long AccountID, Long HomeID, OwnFishingData ownFishingData) {
		syslog("****************************************");
		syslog("***********ownFishingDataEvent***********");
		syslog("****************************************");
	}

	@Override
	public void visitedHomeDataEvent(Long AccountID, Long HomeID, VisitedHomeData visitedHomeData) {
		syslog("****************************************");
		syslog("***********visitedHomeDataEvent***********");
		syslog("****************************************");

	}

	@Override
	public Object tickActionEvent(Long AccountID, Long HomeID, EndClientTurn ectm, int beginT, int endT) {
		syslog("****************************************");
		syslog("***********tickActionEvent***********");
		syslog("****************************************");

//		int lastTick = beginT, cmdcount = 0;

//		int uses = this.farm.collectHarvest(ectm, lastTick, cmdcount, this.showSyslog);
//		Field wheat = new Field(GlobalID.create(4, 1));
//		lastTick += uses * 2;
//		cmdcount += uses;

//		this.farm.sowSeeds(wheat.id(), 20, wheat.timeSec(), ectm, lastTick, this.showSyslog);
//
//		List<Integer> list = new ArrayList<>();
//		list.add(wheat.id());

//		this.farm.sellSlot(ectm, lastTick, list, true, wheat.id(), true);
//		System.out.println(this.farm.getShop().getSlot(0).getAsJsonObject());

//		public int sellSlot(EndClientTurn ectm, int lastTick, List<Integer> tosell, boolean sellPriceMin, Integer mainCop, boolean showlog) {


		return null;
	}

	@Override
	public void endClientTurnOkEvent(Long AccountID, Long HomeID, EndClientTurnOK endClientTurnOK) {
		syslog("****************************************");
		syslog("***********endClientTurnOkEvent***********");
		syslog("****************************************");
//		this.ap.putMessage(new GoHome());

		Long timediff = Long.valueOf((new Date()).getTime() - this.lastReload.getTime());

		if (timediff.longValue() >= 60000L) {
			this.ap.putMessage(new GoHome());
		}
	}

	@Override
	public void messageReceiveEvent(Long AccountID, Long HomeID, Base base) {
		syslog("****************************************");
		syslog("***********messageReceiveEvent***********");
		syslog("****************************************");
		if(this.farm.getOpenBox() != null && 
				this.farm.getOpenBox().state() == MysteryBoxState.NotChecked) {
			this.ap.putMessage(new GoHome());
		}

	}

	@Override
	public void unknownMessageReceiveEvent(Long AccountID, Long HomeID, Unknown unknown) {
		syslog("****************************************");
		syslog("***********unknownMessageReceiveEvent***********");
		syslog("****************************************");

	}

	@Override
	public Base loddegInEvent(Long AccountID, Long HomeID, LoginOK loginOK) {
		syslog("****************************************");
		syslog("***********ownFishingDataEvent**********");
		syslog(Logger.formatPT(this.name, "  logged in."));
		syslog("****************************************");
		return new GoHome();
	}

	@Override
	public void stoppedEvent(Long AccountID, Long HomeID) {
		syslog("****************************************");
		syslog("***********ownFishingDataEvent***********");
		syslog(Logger.formatPT(this.name, "  worker stopped."));
		syslog("****************************************");
	}

	@Override
	public void updateLoginData(Long AccountID, Long HomeID, String loginData) {
		syslog("Update Login Data:");
		syslog(loginData);
		Login nld = new Login(StringUtils.stringToByteArray(loginData));
		nld.HomeID = HomeID;
	}

	@Override
	public void messageSentEvent(Long AccountID, Long HomeID, Object o) {
		syslog("****************************************");
		syslog("***********messageSentEvent***********");
		syslog("****************************************");
		ap.putMessage(new GoHome());
	}


	private void syslog(String log) {
		if (this.showSyslog) System.out.println(log);
	}
}
